package com.mathset.business.service.impl;



import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import com.mathset.business.dao.IMailDao;
import com.mathset.business.service.IMailService;

@Service
public class MailService implements IMailService  {

	@Autowired
	private IMailDao iMailDao;
	
	@Autowired
	private JavaMailSenderImpl javaMailSenderImpl;
	
	@Autowired
	private SimpleMailMessage mailMessage;
	
	
	private static String VALIDATING_ADDRESS = "http://localhost:8080/mathset/mail/validateMail?token=";
	
	private Logger logger = Logger.getLogger(MailService.class);
	
	public void sendMail(String to, String token) {
	    mailMessage.setTo(to); 
	    mailMessage.setText( VALIDATING_ADDRESS + token); 
	    javaMailSenderImpl.send(mailMessage);
        logger.info("send mail to : " + to + "successfully.");
	}

	@Override
	public String validateMail(String token) {
		return iMailDao.retrieveMailAddressByMailToken(token);
	}

	@Override
	public void updateMemberMailValidated(String email) {
		iMailDao.updateMemberMailValidated(email);
	}


}
